"""
Case Type   : 导入导出、备份恢复工具支持M*兼容性
Case Name   : gs_dump工具导出兼容M*数值类型
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.兼容b库下建表，含兼容数值类型
    3.使用gs_dump工具导出表为自定义归档格式
    4.导入之前导出的数据到新库
    5.连接导入库查询
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
History     :modified by zhanghuan96,2024.6.28，无符号类型显示带精度
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0002(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.db_name = "db_dump_0002"
        self.db_name_01 = "db_dump_0002_01"
        self.tb_name = "tb_dump_0002"
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0002')
        self.expect_result_dict = {
            'Column': ['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9',
                       'c10', 'c11', 'c12', 'c13', 'c14', 'c15', 'c16', 'c17'],
            'Type': ['uint1(20)', 'uint2(20)', 'uint4(20)', 'uint4(20)',
                     'uint4(20)', 'uint8(20)',
                     'tinyint', 'smallint', 'integer', 'integer', 'integer',
                     'bigint', 'uint1', 'uint2', 'uint4', 'uint8', 'uint4']}

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';" \
                  f"drop database if exists {self.db_name_01};" \
                  f"create database {self.db_name_01} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertEqual(2, sql_result.count(
            self.constant.CREATE_DATABASE_SUCCESS))

        text = '----step2: 兼容b库下建表，含兼容数值类型 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
                  f"create table {self.tb_name}(" \
                  f"c1 tinyint(20)unsigned zerofill," \
                  f"c2 smallint(20)unsigned zerofill," \
                  f"c3 mediumint(20)unsigned zerofill," \
                  f"c4 int(20)unsigned zerofill," \
                  f"c5 integer(20)unsigned zerofill," \
                  f"c6 bigint(20)unsigned zerofill," \
                  f"c7 tinyint," \
                  f"c8 smallint," \
                  f"c9 mediumint," \
                  f"c10 int," \
                  f"c11 integer," \
                  f"c12 bigint," \
                  f"c13 tinyint zerofill," \
                  f"c14 smallint zerofill," \
                  f"c15 mediumint zerofill," \
                  f"c16 bigint zerofill," \
                  f"c17 integer zerofill)engine='字符';" \
                  f"insert {self.tb_name} values(255,65535,4294967295," \
                  f"4294967295,4294967295,18446744073709551615,-128,32767," \
                  f"2147483647,-2147483648,2147483647,9223372036854775807,0," \
                  f"65535,4294967295,18446744073709551615,4294967295);"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_result)

        text = '----step3: 使用gs_dump工具导出表为自定义归档格式 expect: 成功----'
        self.log.info(text)
        gs_dump_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gs_dump " \
                      f"-p {self.dbuser_node.db_port} " \
                      f"{self.db_name} -F c " \
                      f"-f {self.dump_path}"
        self.log.info(gs_dump_cmd)
        dump_msg = self.dbuser_node.sh(gs_dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(f"dump database {self.db_name} successfully",
                      dump_msg, '执行失败:' + text)

        text = '----step4: 导入之前导出的数据到新库 expect: 成功----'
        self.log.info(text)
        gs_restore_cmd = f"source {macro.DB_ENV_PATH};" \
                         f"gs_restore -p {self.dbuser_node.db_port} " \
                         f"-d {self.db_name_01} " \
                         f"{self.dump_path}"
        self.log.info(gs_restore_cmd)
        restore_msg = self.dbuser_node.sh(gs_restore_cmd).result()
        self.log.info(restore_msg)
        self.assertIn(self.constant.RESTORE_SUCCESS_MSG, restore_msg,
                      '执行失败:' + text)

        text = '----step5: 连接导入库查询 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"\d+ {self.tb_name};"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        result_dict = self.com.format_sql_result(sql_result)
        self.log.info(result_dict)
        del_key = ['Modifiers', 'Storage', 'Stats target', 'Description']
        for k in del_key:
            result_dict.pop(k, None)
        self.assertDictEqual(self.expect_result_dict, result_dict)

        sql_cmd = f"select * from {self.tb_name};"
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        msg_lis = ['255', '65535', '4294967295', '4294967295', '4294967295',
                   '18446744073709551615', '-128', '32767', '2147483647',
                   '-2147483648', '2147483647', '9223372036854775807', '0',
                   '65535', '4294967295', '18446744073709551615', '4294967295']
        for msg in msg_lis:
            self.assertIn(msg, sql_result, '查询失败')

    def tearDown(self):
        text = '---step6: 清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name_01};"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        self.assertEqual(2,
                         sql_result.count(self.constant.DROP_DATABASE_SUCCESS))
        self.assertEqual('', rm_msg)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
